問題描述
Wagtail:如何設置單元測試以進行簡單的頁面編輯? (Wagtail: How to setup up unittest for simple page edit?)
我正在嘗試使用 django‑pytest 和 wagtail_factories 在 Wagtail 中設置一個簡單的測試,以測試用戶編輯 Pages
他們的能力自己的。但是當我嘗試獲取編輯網址時,我一直在重定向。(對於我這裡的示例,我使用超級用戶固定裝置不必處理權限。)
# test_views.py
def test_user_can_edit_owned_pages(client, superuser):
parent_page = MyPageIndexPageFactory()
my_page = MyPageFactory(owner=superuser, parent=parent_page)
edit_url = reverse("register_mypage_modeladmin_edit", args=[my_page.pk])
client.force_login(superuser)
response = client.get(edit_url)
# to capture in pytests output
print(
superuser.is_superuser,
my_page.id,
response
)
assert response.status_code == 200
assert
失敗,儘管捕獲的輸出顯示 Page get 在 url 中使用正確的 ID 創建 ‑ 但我仍然在重定向。
True 4778 <HttpResponseRedirect status_code=302, "text/html; charset=utf‑8", url="/admin/pages/4778/edit/?next=/admin/my_app/mypage/">
當我獲得模型列表視圖的索引 url 時,一切都很好。
[...]
index_url = reverse("register_mypage_modeladmin_index")
response = client.get(index_url)
qs = response.context[0]["object_list"]
print(qs)
捕獲的輸出:]>
我懷疑這(仍然)是一個權限問題,即使我使用的是 superuser
。如何在 Wagtail 中設置這樣的簡單測試?
參考解法
方法 1:
The URL route you're accessing (register_mypage_modeladmin_edit
) is part of the modeladmin module, which does indeed redirect to the standard page edit view when you use it on a page model:
For Page models, the system directs to Wagtail’s existing add and edit views, and returns you back to the correct list page, for a seamless experience.
So, what you're seeing is Modeladmin working as designed. Perhaps you intended to run this test against the standard page edit view instead? If so, the URL route you want is wagtailadmin_pages:edit
.
(by Jean Zombie、gasman)
參考文件